﻿using System;
using System.Windows;
using System.Windows.Media.Imaging;
using Esponce.QRCode;

namespace Esponce.Models
{
	public class EncodeViewModel : GenerateViewModel
	{
		#region Public methods
		public void Encode(string content)
		{
			try
			{
				//Make an API request
				var model = GetModel();
				var client = new QRCodeClient();
				client.Format = App.ViewModel.SelectedDataFormat;
				var stream = client.Encode(model);

				//Convert byte array to image
				var decoder = new PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
				var source = decoder.Frames[0];
				this.ImageSource = source;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
			}
		}
		#endregion
	}
}
